package dolf.objects;

import java.awt.Image;
import java.net.URL;
import java.util.HashMap;



public class ImageStore implements java.io.Serializable{
	private static final long serialVersionUID = 1L;
	private static ImageStore instance = new ImageStore();
	private HashMap<String, javax.swing.ImageIcon> icons = new HashMap<String, javax.swing.ImageIcon>();
	
	
	public static ImageStore getInstance() {
		return instance;
	}
	
	private javax.swing.ImageIcon loadIcon(String filename) {
		javax.swing.ImageIcon icon = null;
		try {
			URL url = this.getClass().getClassLoader().getResource(filename);
			if (url == null) {
				System.err.println("Can't find ref: "+filename);
			}
	    
			icon = new javax.swing.ImageIcon(url);
		}
		catch (Exception e) {
			System.err.println("failed to load Icon: "+filename);
		}
	    return icon;
	}
	
	public Image getImage(String filename) {

		if (icons.get(filename) != null) {
			return icons.get(filename).getImage();
		}
		
		// loader
		System.out.println("loading Image " + filename);
		javax.swing.ImageIcon icon = loadIcon(filename);
		
		if (icon != null) {
			icons.put(filename,icon);
			return icon.getImage();
		} else return null;
		
		// create a sprite, add it the cache then return it
		
		
	}
	public javax.swing.ImageIcon getIcon(String filename) {

		if (icons.get(filename) != null) {
			return (javax.swing.ImageIcon) icons.get(filename);
		}
		
		// loader
		System.out.println("loading Icon " + filename);
		javax.swing.ImageIcon icon = loadIcon(filename);
		
		if (icon != null) {
			icons.put(filename,icon);
			return icon;
		} else return null;
		
		// create a sprite, add it the cache then return it
		
		
	}
}